package com.example.jinzproduct.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commons.entity.*;
import com.example.commons.utils.ApiResult;
import com.example.jinzproduct.RedisUtil.RedisUtil;
import com.example.jinzproduct.server.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresAuthentication;

import javax.annotation.Resource;

import static com.example.commons.utils.TokenTag.TOKEN_TAG;
import static com.example.jinzproduct.util.ChangeChinesePinyin.changeChinesePinyin;

/**
 * 首页接口控制类
 *
 */
@RestController
@RequestMapping("/jinZhiIndex")
public class IndexController {

    @Autowired
    private IndexService indexService;
    @Autowired
    private HotSearchService hotSearchService;
    @Autowired
    private ConsultationsService consultationsService;
    @Autowired
    private UserService userService;
    @Autowired
    private MsgService msgService;
       @Resource
       private RedisUtil redisUtil;
    /**
     * 首页接口轮播图接口
     * @return
     */
    @GetMapping("/banners")
    public Object indexInfo(){


        return ApiResult.success(indexService.indexInfo());
    }

    /**
     * 首页公告接口
     * @return
     */
    @GetMapping("/indexArticle")
    @RequiresAuthentication
    @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
    public Object indexArticle(String secondColumn , @RequestParam(value="current", required = false,defaultValue = "1") Integer current , @RequestParam(value="number", required = false,defaultValue = "10") Integer number){

        Map parameterMap =new HashMap();
        parameterMap.put("current",current);
        parameterMap.put("number",number);
        parameterMap.put("secondColumn",secondColumn);

        String key = redisUtil.createRediskey(parameterMap, "indexArticle");
        Map ca = (Map) RedisUtil.get(key);
        if (ca != null){
            return ApiResult.success(ca);
        }
        Map map= consultationsService.findList(current,number,secondColumn);
        redisUtil.set(key,map,18000); //五小时过期
        return ApiResult.success(map);
    }





    /**
     * 院校动态，站点文章查询
     * @return
     */
    @GetMapping("/siteArticle")
    public ApiResult siteArticle(String orgName,Integer current, Integer number){
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.like("contents",orgName);
        queryWrapper.orderByDesc("creater_date");
        IPage<ConsultationsDO> page = new Page<>();
        page.setCurrent(current);
        page.setTotal(number);
        IPage listPage = consultationsService.page(page, queryWrapper);
        return ApiResult.success(listPage);
    }



    /**
     * 首页公告查看详情接口
     * @return
     */
    @GetMapping("/articleDetails")
    public Object articleDetails(String id ,String titleName,String newType){
            if(StringUtils.isBlank(titleName)){
                ConsultationsDO consultationsDO = consultationsService.getById(id);
                if (newType!=null) {
                    if (consultationsDO != null) {
                        consultationsDO.setNewType(newType);
                    }
                }
                return ApiResult.success(consultationsDO);
            }else {
                QueryWrapper<ConsultationsDO> wrapper=new QueryWrapper();
                wrapper.like("title",titleName);
                List<ConsultationsDO> serviceOne= consultationsService.list(wrapper);
                if (newType!=null) {
                    if (serviceOne.size() > 0) {
                        for (int i = 0; i < serviceOne.size(); i++) {
                            serviceOne.get(i).setNewType(newType);
                        }
                    }
                }
                return ApiResult.success(serviceOne);
            }

    }

    /**
     * 首页热搜接口
     * @return
     */
    @GetMapping("/hotSearch")
    public Object hotSearch( ){
        QueryWrapper<HotSearchDO> wrapper = new QueryWrapper<HotSearchDO>().orderByDesc("id");
        return ApiResult.success(hotSearchService.list(wrapper));
    }


    /**
     * 首页消息
     * @return
     */
    @GetMapping("/getMsgList")
    public Object getMsgList(String token ){

      UserDO userDO= userService.getUserInfo(token);
        if(userDO==null){
            return ApiResult.fail("未查询到信息");
        }
        QueryWrapper<MsgDO >msgquery=new QueryWrapper<>();
        msgquery.eq("userId",userDO.getId());
        List<MsgDO> list=  msgService.list(msgquery);
        return list;
    }



}
